package com.health.tablebean.webservice;

import android.content.Context;
import android.text.TextUtils;

import com.health.tablebean.ARCHIVE_BASEINFO;
import com.health.viewscontrols.NovaApplication;
import com.health.db.FDbManager;
import com.health.tablebean.CD_DRUGCONDITION;
import com.health.tablebean.CD_MENTALDISEASE_FOLLOWUP;
import com.health.tablebean.PharmacyList;
import com.health.tools.GlobalConts;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 田相伟 on 2018/3/31.
 * <p>
 * 精神病随访
 */

public class Jsjbsfb {
    private String cardid;//身份证号
    private String res2;//危险行为 无:DM01-11_1
    private String createorg;//创建机构
    private String wzd;//完整度
    private String res4;//本次随访形式门诊：DM04-04_1家庭访视：DM04-04_2电话：DM04-04_3

    private String createtime;//创建时间
    private String updatetime;//修改时间
    private String creater;//创建人
    private String modified;//修改人
    private String updateorg;//修改机构
    private String qdqxz;//缺项值
    private String fromType = "2";// 数据来源；空为公卫，‘1’为手提箱，‘2’为PAD
    private int res9;//其他危害行为次数
    private Rollout rollOut;
    //    private String hpc;//转诊原因
//    private String rollInRegion;//机构及科室
    private int selfInjury;//自伤次数
    private String nofollowUp;//失访原因
    private String res6;//死亡原因 躯体疾病
    private String res7;//死亡原因
    private String happentime;//随访日期
    private String otherSymptom;//目前症状其他
    private String selfEvaluation;//自知力自知力完全:DM01-74_1自知力不全:DM01-74_2自知力缺失:DM01-74_3

    private String sleepStatus;//睡眠情况良好：DM01-69_1一般：DM01-69_2较差：DM01-69_4

    private String dietaryStatus;//饮食情况良好：DM01-69_1一般：DM01-69_2较差：DM01-69_4

    private String workAbility;//生产劳作及工作良好：DM01-69_1一般：DM01-69_2较差：DM01-69_4此项不适用：DM01-69_5

    private String studyAbility;//学习能力良好：DM01-69_1一般：DM01-69_2较差：DM01-69_4

    private String commuAbility;//社会人际交往良好：DM01-69_1一般：DM01-69_2较差：DM01-69_4

    private String hospitalization;//两次随访期间住院情况未住院:DM01-76_1目前正在住院:DM01-76_2曾住院，现未住院:DM01-76_3

    private String dischargeDate;//末次出院时间
    private String labExamMark;//实验室检查
    private String labExamStatus;//实验室检查描述
    private String mediCompliance;//用药依从性：按医嘱规律用药：DM01-111_1间断用药：DM01-111_2不用药：DM01-111_3医嘱勿需用药：DM01-111_4

    private String adreactionMark;//药物不良反应无：DM01-112_1有：DM01-112_2此项不适用：DM01-112_3

    private String rehabilitation;//康复措施生活劳动能力：DM07-03_1职业训练：DM07-03_2学习能力：DM07-03_3社会交往：DM07-03_4其他：DM07-03_9

    private String followupEva;//本次随访分类不稳定：DM01-71_5基本稳定：DM01-71_6稳定：DM01-71_7

    private String rehabilitaGuide;//康复措施其他
    private String nextVisitDate;//下次随访时间
    private List<PharmacyList> pharmacyList;// dosage 用量
    // drugName 药物名称
    // reg2 用药单位
    // usage 用法
    private List<PharmacyList> pharmacyListzd;// dosage 用量
    // drugName 药物名称
    // reg2 用药单位
    // usage 用法


    private String mainSymptom;//目前状况
    private int lightAffray;//轻度滋事次数
    private int causeTrouble;//肇事次数


    private int causeAccident;//肇祸次数
    private int attemptedSuicide;//自杀未遂次数
    private String pasttreatEffect;//治疗效果 痊愈：DM01-77_1 好转：DM01-77_2 无变化：DM01-77_3 加重：DM01-77_4 此项不适用：DM01-77_5

    private String inputMan;//随访医生签名
    private String selfCare;//个人生活料理 良好：DM01-69_1 一般：DM01-69_2 较差：DM01-69_4

    private String housework;//家务劳动 良好：DM01-69_1 一般：DM01-69_2 较差：DM01-69_4

    private String lockCode;//两次随访期间关锁情况 无关锁：DM01-75_1 关锁：DM01-75_2 关锁已解除：DM01-75_3

    private String hazardEva;//危险项评估 0级：DM07-02_1 1级：DM07-02_2 2级：DM07-02_3 3级：DM07-02_4 4级：DM07-02_5 5级：DM07-02_6

    private String adReaction;//药物不良反用描述
    private String referral;//是否转诊  否:DM01-10_1 是:DM01-10_2

    private String res10;//死亡原因其他

    public String getFromType() {
        return fromType;
    }

    public void setFromType(String fromType) {
        this.fromType = fromType;
    }

    public String getCardid() {
        return cardid;
    }

    public void setCardid(String cardid) {
        this.cardid = cardid;
    }

    public String getRes2() {
        return res2;
    }

    public void setRes2(String res2) {
        this.res2 = res2;
    }

    public String getCreateorg() {
        return createorg;
    }

    public void setCreateorg(String createorg) {
        this.createorg = createorg;
    }

    public String getWzd() {
        return wzd;
    }

    public void setWzd(String wzd) {
        this.wzd = wzd;
    }

    public String getRes4() {
        return res4;
    }

    public void setRes4(String res4) {
        this.res4 = res4;
    }

    public String getCreatetime() {
        return createtime;
    }

    public void setCreatetime(String createtime) {
        this.createtime = createtime;
    }

    public String getUpdatetime() {
        return updatetime;
    }

    public void setUpdatetime(String updatetime) {
        this.updatetime = updatetime;
    }

    public String getCreater() {
        return creater;
    }

    public void setCreater(String creater) {
        this.creater = creater;
    }

    public String getModified() {
        return modified;
    }

    public void setModified(String modified) {
        this.modified = modified;
    }

    public String getUpdateorg() {
        return updateorg;
    }

    public void setUpdateorg(String updateorg) {
        this.updateorg = updateorg;
    }

    public String getQdqxz() {
        return qdqxz;
    }

    public void setQdqxz(String qdqxz) {
        this.qdqxz = qdqxz;
    }

    public int getRes9() {
        return res9;
    }

    public void setRes9(int res9) {
        this.res9 = res9;
    }

    public Rollout getRollOut() {
        return rollOut;
    }

    public void setRollOut(Rollout rollOut) {
        this.rollOut = rollOut;
    }

    public int getSelfInjury() {
        return selfInjury;
    }

    public void setSelfInjury(int selfInjury) {
        this.selfInjury = selfInjury;
    }

    public String getNofollowUp() {
        return nofollowUp;
    }

    public void setNofollowUp(String nofollowUp) {
        this.nofollowUp = nofollowUp;
    }

    public String getRes6() {
        return res6;
    }

    public void setRes6(String res6) {
        this.res6 = res6;
    }

    public String getRes7() {
        return res7;
    }

    public void setRes7(String res7) {
        this.res7 = res7;
    }

    public String getHappentime() {
        return happentime;
    }

    public void setHappentime(String happentime) {
        this.happentime = happentime;
    }

    public String getOtherSymptom() {
        return otherSymptom;
    }

    public void setOtherSymptom(String otherSymptom) {
        this.otherSymptom = otherSymptom;
    }

    public String getSelfEvaluation() {
        return selfEvaluation;
    }

    public void setSelfEvaluation(String selfEvaluation) {
        this.selfEvaluation = selfEvaluation;
    }

    public String getSleepStatus() {
        return sleepStatus;
    }

    public void setSleepStatus(String sleepStatus) {
        this.sleepStatus = sleepStatus;
    }

    public String getDietaryStatus() {
        return dietaryStatus;
    }

    public void setDietaryStatus(String dietaryStatus) {
        this.dietaryStatus = dietaryStatus;
    }

    public String getWorkAbility() {
        return workAbility;
    }

    public void setWorkAbility(String workAbility) {
        this.workAbility = workAbility;
    }

    public String getStudyAbility() {
        return studyAbility;
    }

    public void setStudyAbility(String studyAbility) {
        this.studyAbility = studyAbility;
    }

    public String getCommuAbility() {
        return commuAbility;
    }

    public void setCommuAbility(String commuAbility) {
        this.commuAbility = commuAbility;
    }

    public String getHospitalization() {
        return hospitalization;
    }

    public void setHospitalization(String hospitalization) {
        this.hospitalization = hospitalization;
    }

    public String getDischargeDate() {
        return dischargeDate;
    }

    public void setDischargeDate(String dischargeDate) {
        this.dischargeDate = dischargeDate;
    }

    public String getLabExamMark() {
        return labExamMark;
    }

    public void setLabExamMark(String labExamMark) {
        this.labExamMark = labExamMark;
    }

    public String getLabExamStatus() {
        return labExamStatus;
    }

    public void setLabExamStatus(String labExamStatus) {
        this.labExamStatus = labExamStatus;
    }

    public String getMediCompliance() {
        return mediCompliance;
    }

    public void setMediCompliance(String mediCompliance) {
        this.mediCompliance = mediCompliance;
    }

    public String getAdreactionMark() {
        return adreactionMark;
    }

    public void setAdreactionMark(String adreactionMark) {
        this.adreactionMark = adreactionMark;
    }

    public String getRehabilitation() {
        return rehabilitation;
    }

    public void setRehabilitation(String rehabilitation) {
        this.rehabilitation = rehabilitation;
    }

    public String getFollowupEva() {
        return followupEva;
    }

    public void setFollowupEva(String followupEva) {
        this.followupEva = followupEva;
    }

    public String getRehabilitaGuide() {
        return rehabilitaGuide;
    }

    public void setRehabilitaGuide(String rehabilitaGuide) {
        this.rehabilitaGuide = rehabilitaGuide;
    }

    public String getNextVisitDate() {
        return nextVisitDate;
    }

    public void setNextVisitDate(String nextVisitDate) {
        this.nextVisitDate = nextVisitDate;
    }

    public List<PharmacyList> getPharmacyList() {
        return pharmacyList;
    }

    public void setPharmacyList(List<PharmacyList> pharmacyList) {
        this.pharmacyList = pharmacyList;
    }

    public List<PharmacyList> getPharmacyListzd() {
        return pharmacyListzd;
    }

    public void setPharmacyListzd(List<PharmacyList> pharmacyListzd) {
        this.pharmacyListzd = pharmacyListzd;
    }

    public String getMainSymptom() {
        return mainSymptom;
    }

    public void setMainSymptom(String mainSymptom) {
        this.mainSymptom = mainSymptom;
    }

    public int getLightAffray() {
        return lightAffray;
    }

    public void setLightAffray(int lightAffray) {
        this.lightAffray = lightAffray;
    }

    public int getCauseTrouble() {
        return causeTrouble;
    }

    public void setCauseTrouble(int causeTrouble) {
        this.causeTrouble = causeTrouble;
    }

    public int getCauseAccident() {
        return causeAccident;
    }

    public void setCauseAccident(int causeAccident) {
        this.causeAccident = causeAccident;
    }

    public int getAttemptedSuicide() {
        return attemptedSuicide;
    }

    public void setAttemptedSuicide(int attemptedSuicide) {
        this.attemptedSuicide = attemptedSuicide;
    }

    public String getPasttreatEffect() {
        return pasttreatEffect;
    }

    public void setPasttreatEffect(String pasttreatEffect) {
        this.pasttreatEffect = pasttreatEffect;
    }

    public String getInputMan() {
        return inputMan;
    }

    public void setInputMan(String inputMan) {
        this.inputMan = inputMan;
    }

    public String getSelfCare() {
        return selfCare;
    }

    public void setSelfCare(String selfCare) {
        this.selfCare = selfCare;
    }

    public String getHousework() {
        return housework;
    }

    public void setHousework(String housework) {
        this.housework = housework;
    }

    public String getLockCode() {
        return lockCode;
    }

    public void setLockCode(String lockCode) {
        this.lockCode = lockCode;
    }

    public String getHazardEva() {
        return hazardEva;
    }

    public void setHazardEva(String hazardEva) {
        this.hazardEva = hazardEva;
    }

    public String getAdReaction() {
        return adReaction;
    }

    public void setAdReaction(String adReaction) {
        this.adReaction = adReaction;
    }

    public String getReferral() {
        return referral;
    }

    public void setReferral(String referral) {
        this.referral = referral;
    }

    public String getRes10() {
        return res10;
    }

    public void setRes10(String res10) {
        this.res10 = res10;
    }

    public static List<Jsjbsfb> convertData(Context context) {
        List<CD_MENTALDISEASE_FOLLOWUP> datas = FDbManager.getInstance(context).findAllByWhere(
                CD_MENTALDISEASE_FOLLOWUP.class, "IS_UPDATE is not 'N'" + " AND CREATEORG='" + NovaApplication.getJgbh() + "'");
        List<Jsjbsfb> wbdatas = new ArrayList<Jsjbsfb>();
        Jsjbsfb wbdata;
        for (CD_MENTALDISEASE_FOLLOWUP data : datas) {
            wbdata = new Jsjbsfb();
            if("null".equals(data.getATTEMPT_SUICIDE_NONE())){//无危险行为
                wbdata.setRes2("DM01-11_1");
            }
            wbdata.setCardid(TextUtils.isEmpty(data.getIDCARD()) ? "" : data.getIDCARD().toUpperCase());// 居民身份证/健康卡号
            if(TextUtils.isEmpty(wbdata.getCardid())&&!TextUtils.isEmpty(data.getARCHIVEID())){
                List<ARCHIVE_BASEINFO> ss=FDbManager.getInstance(context).findAllByWhere(ARCHIVE_BASEINFO.class,"ARCHIVEID='"+data.getARCHIVEID()+"'");
                if(ss.size()>0){
                    if(!ss.get(0).getPOPULATION_TYPE().contains("5")){
                        continue;
                    }
                    wbdata.setCardid(ss.get(0).getIDCARD().toUpperCase());
                }else{
                    wbdata.setCardid("");
                }

            }else{
                List<ARCHIVE_BASEINFO> ss=FDbManager.getInstance(context).findAllByWhere(ARCHIVE_BASEINFO.class,"IDCARD='"+data.getIDCARD()+"'");
                if(ss.size()>0){
                    if(!ss.get(0).getPOPULATION_TYPE().contains("5")){
                        continue;
                    }
                }
            }
            wbdata.setCreateorg(data.getCREATEORG());// 创建机构
            wbdata.setCreater(data.getCREATER());// 创建人id CREATED_BY
            wbdata.setUpdatetime(data.getUPDATETIME());
            wbdata.setModified(data.getMODIFIED());
            wbdata.setUpdateorg(data.getUPDATEORG());

            if(data.getCREATED_DATE()!=null){
                wbdata.setCreatetime(GlobalConts.datetimeFormat.format(data.getCREATED_DATE()));// 创建时间
            }else{
                wbdata.setCreatetime("");// 创建时间
            }
            if(data.getOtherDamage_FREQUEN()!=null){
                wbdata.setRes9(data.getOtherDamage_FREQUEN());//其他伤害行为
            }
            if(!TextUtils.isEmpty(data.getAUTOLESION_FREQUEN())){
                wbdata.setSelfInjury(Integer.parseInt(data.getAUTOLESION_FREQUEN()));//自伤次数
            }

            wbdata.setNofollowUp(data.getLossFollow());//失访原因
            wbdata.setRes6(data.getDeathCauseBody());//死亡原因 躯体疾病
            wbdata.setRes7(data.getDeathCause());//死亡原因
            wbdata.setHappentime(data.getFOLLOWUP_DATE()==null?"":GlobalConts.dateFormat.format(data.getFOLLOWUP_DATE()));//随访日期

            wbdata.setOtherSymptom(data.getPRESENT_SYMPTO_OTHER());//目前症状，其他
            wbdata.setSelfEvaluation(data.getINSIGHT());//自知力
            wbdata.setSleepStatus(data.getSLEEP_QUALITY());//睡眠状况
            wbdata.setDietaryStatus(data.getDIET());//饮食情况
            wbdata.setWorkAbility(data.getPRODUCT_LABOR_WORK());//生产劳作及工作
            wbdata.setStudyAbility(data.getLEARNING_ABILITY());//学习能力
            wbdata.setCommuAbility(data.getSOCIAL_INTER_INTERA());//社会人际交往
            wbdata.setHospitalization(data.getHOSPITALIZATI_STATUS());//两次随访期间住院情况
            wbdata.setDischargeDate(data.getLAST_LEAVE_HOSP_TIME()==null?"":GlobalConts.dateFormat.format(data.getLAST_LEAVE_HOSP_TIME()));//末次出院时间
            wbdata.setLabExamMark(data.getLABOR_EXAMINATI());//实验室检查
            wbdata.setLabExamStatus(data.getLABOR_EXAMINATI_HAVE());//实验室检查描述
            wbdata.setMediCompliance(data.getMEDICATIO_COMPLIANCE());//用药依从性
            wbdata.setAdreactionMark(data.getADVER_DRU_REACT());//药物不良反映
            wbdata.setAdReaction(data.getADVER_DRU_REACT_HAVE());// 药物不良反应描述
            wbdata.setRehabilitation(data.getREHABILI_MEASU());//康复措施


            wbdata.setInputMan(data.getFOLLOWUPDOCTOR());// 随访人id
            wbdata.setNextVisitDate(data.getNEXTFOLLOWUP_DATE() == null ? ""
                    : GlobalConts.dateFormat.format(data.getNEXTFOLLOWUP_DATE()));// 下次随访时间
            wbdata.setMainSymptom(data.getPRESENT_SYMPTOM());//目前症状
            if(!TextUtils.isEmpty(data.getMILD_TROUBLE_FREQUEN())){
                wbdata.setLightAffray(Integer.parseInt(data.getMILD_TROUBLE_FREQUEN()));//轻度滋事次数
            }
            if(!TextUtils.isEmpty(data.getCREATE_DISTU_FREQUEN())){
                wbdata.setCauseTrouble(Integer.parseInt(data.getCREATE_DISTU_FREQUEN()));// 肇事次数
            }
            if(!TextUtils.isEmpty(data.getCAUSE_ACCID_FREQUEN())){
                wbdata.setCauseAccident(Integer.parseInt(data.getCAUSE_ACCID_FREQUEN()));//肇祸次数
            }

            if(!TextUtils.isEmpty(data.getATTEMPT_SUIC_FREQUEN())){
                wbdata.setAttemptedSuicide(Integer.parseInt(data.getATTEMPT_SUIC_FREQUEN()));//自杀未遂次数
            }
            wbdata.setPasttreatEffect(data.getTREATMENT_EFFECT());//治疗效果
            wbdata.setSelfCare(data.getPERSONAL_CARE());//个人生活料理
            wbdata.setHousework(data.getHOUSEWORK());//家务劳动
            wbdata.setLockCode(data.getLOCK_CONDITION());//两次随访期间关锁的情况
            wbdata.setHazardEva(data.getFATALNESS());//危险性评估
            wbdata.setReferral(data.getWHETHER_REFERRAL());//是否转诊
            wbdata.setRes10(data.getDeathCause_EX());//死亡原因其他






            Rollout rollout = new Rollout();
            rollout.setHpc(data.getREFERRAL_REASON());
            rollout.setRollInRegion(data.getREFERRAL_AGENC_DEPAR());
            wbdata.setRollOut(rollout);


            if (data.getFOLLOWUP_TYPE().contains("DM04-04")){
                wbdata.setRes4(data.getFOLLOWUP_TYPE().replace("DM04-04","DM04-15"));// 随访方式代码
            }else{
                wbdata.setRes4(data.getFOLLOWUP_TYPE());// 随访方式代码
            }
//            wbdata.setFromType("2");
            wbdata.setUpdatetime(GlobalConts.datetimeFormat.format(data.getLAST_UPDATE_DATE()));

            FDbManager dbm = FDbManager.getInstance(context);
            List<CD_DRUGCONDITION> drs = dbm.findAllByWhere(CD_DRUGCONDITION.class,
                    "IDCARD = '" + data.getIDCARD() + "'  AND TYPE = '3'");
            if (drs != null && !drs.isEmpty()) {
                // wbdata.setPharmacyList(pharmacyList
                List<PharmacyList> phs = new ArrayList<>();
                PharmacyList ph = null;
                for (CD_DRUGCONDITION dr : drs) {
                    ph = new PharmacyList();
                    ph.setDosage(dr.getEVERYTIMEMG());
                    ph.setDrugName(dr.getNAME());
                    if (TextUtils.isEmpty(dr.getRES1())) {
                        ph.setReg2("DM04-05_01");// 默认为mg
                    }else{
                        ph.setReg2(dr.getRES1());
                    }
                    ph.setUsage(dr.getDAILYTIME());
                    phs.add(ph);
                }
                wbdata.setPharmacyList(phs);
            } else {
                wbdata.setPharmacyList(new ArrayList());
            }
            List<CD_DRUGCONDITION> drs2 = dbm.findAllByWhere(CD_DRUGCONDITION.class,
                    "IDCARD = '" + data.getIDCARD() + "'  AND TYPE = '6'");
            if (drs2 != null && !drs2.isEmpty()) {
                // wbdata.setPharmacyList(pharmacyList
                List<PharmacyList> phs = new ArrayList<>();
                PharmacyList ph = null;
                for (CD_DRUGCONDITION dr : drs2) {
                    ph = new PharmacyList();
                    ph.setDosage(dr.getEVERYTIMEMG());
                    ph.setDrugName(dr.getNAME());
                    if (TextUtils.isEmpty(dr.getRES1())) {
                        ph.setReg2("DM04-05_01");// 默认为mg
                    }else{
                        ph.setReg2(dr.getRES1());
                    }
                    ph.setUsage(dr.getDAILYTIME());
                    phs.add(ph);
                }
                wbdata.setPharmacyListzd(phs);
            } else {
                wbdata.setPharmacyListzd(new ArrayList());
            }
            String[] arr = GlobalConts.assessIntegrity(wbdata.getCardid(),wbdata.getCreateorg(),wbdata.getRes4(),wbdata.getCreatetime(),wbdata.getCreater(),wbdata.getHappentime(),wbdata.getNextVisitDate(),wbdata.getInputMan());
            wbdata.setWzd(arr[0]);// 完整度
            wbdata.setQdqxz(arr[1]);// 缺项值
            wbdatas.add(wbdata);
      }
        return wbdatas;
    }
}
